//
//  HMStatus.swift
//  WeiBo14
//
//  Created by HM on 16/3/5.
//  Copyright © 2016年 HM. All rights reserved.
//  微博模型

import UIKit

class HMStatus: NSObject {

    // 如果定义成 Int,在32位的系统架构上为Int32,其最大值是没有微博的id大的,是装不下
    var id: Int64 = 0
    // 微博的内容
    var text: String?
    // 当前微博的创建时间 (字符串)
    var created_at: String? {
        didSet{
            // Tue May 31 17:46:55 +0800 2011
            createdDate = NSDate.sinaDateWithString(created_at)
        }
    }
    
    // 当前微博的创建时间 (NSDate类型)
    var createdDate: NSDate?
    
    // 微博来源
    var source: String?
    // 当前发送此条微博的用户信息
    var user: HMUser?
    // 转发数量
    var reposts_count: Int = 0
    // 评论数量
    var comments_count: Int = 0
    // 点赞数量
    var attitudes_count: Int = 0
    // 转发微博
    var retweeted_status: HMStatus?
    // 微博配图s
    var pic_urls: [HMStatusPictureInfo]?
//    {
//        didSet{
//            if var urls = pic_urls where urls.count == 9 {
//                urls.removeFirst(5)
//                pic_urls = urls
//            }
//        }
//    }
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
            guard let v = value as? [String: AnyObject] else {
                return
            }
            user = HMUser(dict: v)
        }else if key == "retweeted_status" {
            guard let v = value as? [String: AnyObject] else {
                return
            }
            retweeted_status = HMStatus(dict: v)
        }else if key == "pic_urls" {
            guard let values = value as? [[String: AnyObject]] else {
                return
            }
            // 定义临时数组,以记录字典转模型的结果
            var tempArray = [HMStatusPictureInfo]()
            for v in values {
                // 字典转模型
                let pictureInfo = HMStatusPictureInfo(dict: v)
                tempArray.append(pictureInfo)
            }
            self.pic_urls = tempArray
        }else{
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
}
